package com.webull.information.center.carwler.job.splan.frame;

import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;
import java.util.function.Function;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.webull.information.center.carwler.common.model.SpeclNewsPlan;
import com.webull.information.center.carwler.task.handler.StockBloombergNewsHandler;
import com.webull.information.center.carwler.task.handler.StockCNBCNewsHandler;
import com.webull.information.center.carwler.task.handler.StockGoogleNewsHandler;
import com.webull.information.center.carwler.task.handler.StockMctrolInNewsHandler;
import com.webull.information.center.carwler.task.handler.StockSinaCnNewsHandler;
import com.webull.information.center.carwler.task.handler.StockYahooNewsHandler;

@Component
public class SplanHandlerMapping {
	private Map<String, Handler<?>> handlerMapping = new ConcurrentHashMap<>();
	
	@PostConstruct
	private void init() {

		handlerMapping.put("google-1", // google 单标的物模式
				produeSpHandler(splanConverter::googleConver1, //
						stockGoogleNewsHandler::handlerByStockTitle4Google));

		handlerMapping.put("sinaCn-1", // sinaCn 单标的物模式 中港美
				produeSpHandler(splanConverter::googleConver1, //
						stockSinaCnNewsHandler::handlerByStockTitle4SinaCn));


		handlerMapping.put("yahoo-1", // yahoo 单标的物模式 中港美
				produeSpHandler(splanConverter::googleConver1, //
						stockYahooNewsHandler::handlerByStockTitle4Yahoo));

		handlerMapping.put("cnbc-1", // cnbc 单标的物模式 stock+etf
				produeSpHandler(splanConverter::cnbcConver1, //
						stockCNBCNewsHandler::handlerByStockTitle4CNBC));

		handlerMapping.put("bloomb-1", // bloomb 单标的物模式 stock+etf
				produeSpHandler(splanConverter::bloombConver1, //
						stockBloombergNewsHandler::handlerByStockTitle4Bloomerg));

		handlerMapping.put("mCtrol-1", // mCtrol 单标的物模式
				produeSpHandler(splanConverter::googleConver1, //
						stockMctrolInNewsHandler::handlerByStockTitle4Mcontrol));

	}

	private @Autowired StockMctrolInNewsHandler stockMctrolInNewsHandler;
	private @Autowired StockBloombergNewsHandler stockBloombergNewsHandler;
	private @Autowired SplanConverter splanConverter;
	private @Autowired StockGoogleNewsHandler stockGoogleNewsHandler;
	private @Autowired StockSinaCnNewsHandler stockSinaCnNewsHandler;
	private @Autowired StockYahooNewsHandler stockYahooNewsHandler;
	private @Autowired StockCNBCNewsHandler stockCNBCNewsHandler;

	public void process(SplanTask st) {
		String tname = st.getSource() + "-" + st.getMode();
		Optional.ofNullable(handlerMapping.get(tname))//
				.ifPresent(v -> {
					try {
						v.accept(v.apply(st));
						st.setExecStat(SpeclNewsPlan.ExecStat.done);
					} catch (Exception e) {
						st.setExecStat(SpeclNewsPlan.ExecStat.error);
					}
		});
	}

	private static <T> Handler<T> produeSpHandler(Function<SplanTask, T> func, Consumer<T> ct) {
		return new Handler<T>() {
			@Override
			public T apply(SplanTask tk) {
				return func.apply(tk);
			}
			@Override
			public void accept(T t) {
				if (t != null)
				ct.accept(t);
			}
		};
	}


	private static interface Handler<T> {
		T apply(SplanTask tk);
		void accept(T t);

	}


}
